//
//  WJWVisitorView.swift
//  SwiftWeibo
//
//  Created by 万嘉伟 on 16/7/19.
//  Copyright © 2016年 万嘉伟. All rights reserved.
//

import UIKit
//1定义代理协议
protocol VisitorViewDelegate:NSObjectProtocol{
    //2注册点击的方法
    func visitorViewDidRegister()
    //2登录点击的方法
    func visitorViewDidLogin()
}

class WJWVisitorView: UIView {
    //3.代理属性
    weak var delegate:VisitorViewDelegate?
    //系统默认即使要用纯代码也要
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        self.setupUI()
    }
    /**
     公开方法
        因为按首页来做，其他
     */
    func setupVisitorInfo(imageName:String,message:String){
        //隐藏房子
        houseView.hidden=true
        //将遮盖移动到View的最后面
        self.sendSubviewToBack(coverView)
        //设置图片
        iconView.image=UIImage(named: imageName)
        //设置文字
        messageLabel.text=message
    }
        /**
        让转轮开始工作
        */
        func startAnimation(){
            //核心动画
            let anim = CABasicAnimation(keyPath: "transform.rotation")
            anim.toValue=2 * M_PI
            anim.duration=20
            anim.repeatCount=MAXFLOAT
            //切换到另外底端tabbar时候核心动画会结束
            anim.removedOnCompletion=false
            //将核心动画添加到图层
            iconView.layer.addAnimation(anim, forKey: nil)
        }
    
    /**
     设置UI界面私有方法
     */
        private func setupUI(){
        //添加子控件
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(houseView)
        self.addSubview(messageLabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
            
        //纯代码要关闭Autoresizing
            iconView.translatesAutoresizingMaskIntoConstraints = false
            coverView.translatesAutoresizingMaskIntoConstraints = false
            houseView.translatesAutoresizingMaskIntoConstraints = false
            messageLabel.translatesAutoresizingMaskIntoConstraints = false
            registerButton.translatesAutoresizingMaskIntoConstraints = false
            loginButton.translatesAutoresizingMaskIntoConstraints = false
            
            
            //将约束添加到view上面
            //CenterX
            self.addConstraint(NSLayoutConstraint(item:iconView,attribute: NSLayoutAttribute.CenterX,relatedBy: NSLayoutRelation.Equal,toItem: self,attribute: NSLayoutAttribute.CenterX,multiplier: 1,constant: 0))
            //CenterY父子都往上偏移40
            self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
            // 遮盖
            self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
            self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
            
            //房子的CenterX
            self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
            //房子的CenterY
            self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute:NSLayoutAttribute.CenterY , multiplier: 1, constant: 0))
            // 消息label CenterX
            self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
            // 消息label Top到iconView底部
            self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
            
            // 消息label 宽度
            // 当 toItem = nil, attribute = NotAnAttribute
            self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 260))
            
            // 注册按钮
            // 书写顺序: Leading, Trailing
            self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
            
            self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
            
            self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
            
            self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
            
            // 登录按钮
            self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
            
            self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
            
            self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
            
            self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
    }
    // MARK: - 按钮点击事件
    @objc private func registerButtonClick() {
        // 4.调用代理的方法
        delegate?.visitorViewDidRegister()
    }
    
    @objc private func loginButtonClick() {
        // 4.调用代理的方法
        delegate?.visitorViewDidLogin()
    }
    // 懒加载
    /// 转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    ///遮罩
    private lazy var coverView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 消息label
    private lazy var messageLabel: UILabel = {
        let label = UILabel(frame: CGRectZero)
        // 设置
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.text = "关注一些人,看看有什么惊喜!"
        // 让控件size根据内容自己调整
        label.sizeToFit()
        
        return label
    }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        
        // 设置
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return button
    }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        
        // 设置
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return button
    }()
   
}
